<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\api\validate;

use think\Validate;

class GuestbookValidate extends Validate
{
    protected $rule = [
        'full_name' => 'require',
        'phone'     => 'require|checkPhone',
        'email'     => 'checkEmail',
        'company'   => 'require',
        'address'   => 'require',
        'fax'       => 'checkFax',
        'msg'       => 'require',
    ];
    protected $message = [
        'full_name.require' => '姓名不能为空！',
        'phone.require'     => '电话不能为空！',
        'company.require'   => '公司名称不能为空！',
        'address.require'   => '地址不能为空！',
        'msg.require'       => '其它说明不能为空！',
    ];

    protected $scene = [
//        'add'  => ['user_login,user_pass,user_email'],
//        'edit' => ['user_login,user_email'],
    ];

    //验证手机号码格式
    protected function checkPhone($value, $rule, $data){
        $chars = "/^13[0-9]{1}[0-9]{8}$|14[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|17[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$|19[0-9]{1}[0-9]{8}$/";
        if (preg_match($chars, $value)){
            return true;
        }
        return "请填写正确的联系电话！";
    }

    //验证邮箱格式
    protected function checkEmail($value, $rule, $data){
        $chars = "/([a-z0-9]*[-_.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[.][a-z]{2,3}([.][a-z]{2})?/i";
        if (preg_match($chars, $value)){
            return true;
        }
        return "请填写正确的电子邮箱！";
    }

    protected function checkFax($value, $rule, $data){
        $chars="/(^[0-9]{3,4}-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^([0-9]{3,4})[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/";
        if (preg_match($chars, $value)){
            return true;
        }
        return "请填写正确的传真号码！";
    }

}
